﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Silverlight.Weblog.Client.DAL.Commands
{
    public static class EnterKeyPressed
    {
        private static readonly DependencyProperty EnterKeyPressedCommandBehaviorProperty = 
            DependencyProperty.RegisterAttached(
            "EnterKeyPressedCommandBehavior",
            typeof(TextBoxEnterKeyPressedCommandBehavior),
            typeof(EnterKeyPressed),
            null);

        public static readonly DependencyProperty CommandProperty = 
            DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(EnterKeyPressed),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty = 
            DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(EnterKeyPressed),
            new PropertyMetadata(OnSetCommandParameterCallback));


        public static void SetCommand(TextBox TextBox, ICommand command)
        {
            TextBox.SetValue(CommandProperty, command);
        }

        public static ICommand GetCommand(TextBox TextBox)
        {
            return TextBox.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(TextBox TextBox, object parameter)
        {
            TextBox.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(TextBox TextBox)
        {
            return TextBox.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            TextBox TextBox = dependencyObject as TextBox;
            if (TextBox != null)
            {
                TextBoxEnterKeyPressedCommandBehavior behavior = GetOrCreateBehavior(TextBox);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            TextBox TextBox = dependencyObject as TextBox;
            if (TextBox != null)
            {
                TextBoxEnterKeyPressedCommandBehavior behavior = GetOrCreateBehavior(TextBox);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static TextBoxEnterKeyPressedCommandBehavior GetOrCreateBehavior(TextBox TextBox)
        {
            TextBoxEnterKeyPressedCommandBehavior behavior = TextBox.GetValue(EnterKeyPressedCommandBehaviorProperty) as TextBoxEnterKeyPressedCommandBehavior;
            if (behavior == null)
            {
                behavior = new TextBoxEnterKeyPressedCommandBehavior(TextBox);
                TextBox.SetValue(EnterKeyPressedCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }
}

